/**
 * Created by loyin on 16/12/20.
 */

$(function () {
    console.log($(document.body).width());
    var document_b=$(document.body);
    var address_dialog = $("#address");
    var invoice_dialog = $("#invoice");
    var mask = $(".mask");
    var dialog = $(".dialog");
    var dialog_close = $(".dialog_close");
    var btn_cancel = invoice_dialog.find(".btn_cancel");

    //地址
    var switch_on = $(".switch_on");
    var switch_off = $(".switch_off");
    var address_off = $(".address_cont");

    switch_on.click(function () {
        switch_on.addClass("hidden");
        switch_off.removeClass("hidden");
        address_off.removeClass("address_off");
    })
    switch_off.click(function () {
        switch_off.addClass("hidden");
        switch_on.removeClass("hidden");
        address_off.addClass("address_off");
    })

    var address_item = $(".address_item");

    address_item.hover(function () {
        var index = $(this).index();
        console.log(index)
        $(this).find(".op_btns").css("visibility", "inherit");
        $(this).find(".op_btns").find(".del_addr").click(function () {
            address_item.eq(index).remove();
        })
        $(this).find(".op_btns").find(".edit_addr").click(function () {
            address_dialog.removeClass("hidden");
            mask.removeClass("hidden");
            //getDialog();
        })
    }, function () {
        $(this).find(".op_btns").css("visibility", "hidden");
    })

    address_item.click(function () {
        var index = $(this).index();
        address_item.find(".item").removeClass("item_selected");
        $(this).find(".item").addClass("item_selected");
        //选择地址后点击收起地址
        switch_off.click(function () {
            address_item.hide();
            address_item.eq(index).show();
        })
        switch_on.click(function () {
            address_item.show();
        })
    })
    $(".add_addr").click(function(){
        address_dialog.removeClass("hidden");
        mask.removeClass("hidden");
        //getDialog();
    })

//    支付方式
    var payment_item=$(".payment_item");
    payment_item.click(function(){
        payment_item.removeClass("item_selected")
        $(this).addClass("item_selected")
    })
//发票
    $(".invoice_cont").find("a").click(function(){
        invoice_dialog.removeClass("hidden");
        mask.removeClass("hidden");
        $(window).scrollTop(0);
        //getDialog();
    })
//    优惠券
    var coupons_use=$(".order_virtual");

    $(".coupons_use").click(function(){
        if (coupons_use.hasClass("hidden")){
            coupons_use.removeClass("hidden");
        }else{
            coupons_use.addClass("hidden");
        }
    })
//    弹窗
    dialog_close.click(function () {
        dialog.addClass("hidden");
        mask.addClass("hidden");
    })
    btn_cancel.click(function () {
        dialog.addClass("hidden");
        mask.addClass("hidden");
    })
dialogL();
    function dialogL(){
        var window_w=$(document.body).width();
        console.log(window_w)
        var dialog_w=dialog.width();
        console.log(dialog_w)
        var _cha=window_w-dialog_w;
        console.log(_cha)
        var _left;
        if(_cha<=0){
            _left=0;
        }else{
            _left=_cha/2;
        }
        dialog.css("left",_left)
    }

    $(window).resize(function () {
        dialogL();
    })
})